package com.example.poverty.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.poverty.entity.OrganizationDepartment;
import com.example.poverty.service.OrganizationDepartmentService;
import com.example.poverty.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 前端 主管单位 控制类
 */
@RestController
@RequestMapping("/organization")
public class OrganizationDepartmentController {

    @Autowired
    private OrganizationDepartmentService organizationDepartmentService;

    /**
     * 添加管理组织
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addOrganizationDepartment(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String principal = request.getParameter("principal").trim();  //将官负责人
        String zucunTaskForce = request.getParameter("zucunTaskForce").trim();  //驻村工作队
        String unit = request.getParameter("unit").trim();  //来自单位

        OrganizationDepartment organizationDepartment = new OrganizationDepartment();
        organizationDepartment.setPrincipal(principal);
        organizationDepartment.setZucunTaskForce(zucunTaskForce);
        organizationDepartment.setUnit(unit);
        boolean flag = organizationDepartmentService.insert(organizationDepartment);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 修改管理组织
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateOrganizationDepartment(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();  //主键
        String principal = request.getParameter("principal").trim();  //将官负责人
        String zucunTaskForce = request.getParameter("zucunTaskForce").trim();  //驻村工作队
        String unit = request.getParameter("unit").trim();  //来自单位

        OrganizationDepartment organizationDepartment = new OrganizationDepartment();
        organizationDepartment.setId(Integer.parseInt(id));
        organizationDepartment.setPrincipal(principal);
        organizationDepartment.setZucunTaskForce(zucunTaskForce);
        organizationDepartment.setUnit(unit);
        boolean flag = organizationDepartmentService.update(organizationDepartment);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "修改失败");
        return jsonObject;
    }

    /**
     * 删除管理组织
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteOrganizationDepartment(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        boolean flag = organizationDepartmentService.delete(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据主键查询管理组织
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        return organizationDepartmentService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查询全部管理组织
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/allOrganizationDepartment", method = RequestMethod.GET)
    public Object allOrganizationDepartment(HttpServletRequest request) {
        return organizationDepartmentService.allOrganizationDepartment();
    }

    /**
     * 根据单位查询相应信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getByUnit", method = RequestMethod.GET)
    public Object getByUnit(HttpServletRequest request) {
        String unit = request.getParameter("unit").trim();  //来自单位
        return organizationDepartmentService.getByUnit(unit);
    }


}
